package com.knowledge.common.utils;


import com.aliyun.searchplat20240529.Client;
import com.aliyun.searchplat20240529.models.GetDocumentSplitRequest;
import com.aliyun.searchplat20240529.models.GetDocumentSplitResponse;
import com.aliyun.searchplat20240529.models.GetDocumentSplitResponseBody;
import com.aliyun.teaopenapi.models.Config;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SpliterDemo2 {

    private static final String API_KEY = "Bearer OS-c8465ieux1wpv3qt"; // 替换为实际的API Key
    private static final String HOST = "http://default-23k7.platform-cn-shanghai.opensearch.aliyuncs.com"; // 替换为实际的服务地址
    private static final String WORKSPACE_NAME = "default"; // 替换为实际的工作空间名称
    private static final String SERVICE_ID = "ops-document-split-001";

    public static void main(String[] args) throws Exception {
        Config config = new Config();
        config.setBearerToken("OS-c8465ieux1wpv3qt");
        config.setEndpoint("default-23k7.platform-cn-shanghai.opensearch.aliyuncs.com");
        config.setProtocol("http");
        Client client = new Client(config);

        GetDocumentSplitRequest request = GetDocumentSplitRequest.build(new HashMap<String, Map<String, Object>>() {{
            put("document", new HashMap<String, Object>() {{
                put("content", "");
                put("content_type", "text");
            }});
            put("strategy", new HashMap<String, Object>() {{
                put("max_chunk_size", 100);
                put("need_sentence", true);
            }});
        }});

        GetDocumentSplitResponse response = client.getDocumentSplit("default", "ops-document-split-001", request);
        Integer statusCode = response.getStatusCode();
        if (200 == statusCode) {
            System.out.println(Arrays.toString(response.getBody().getResult().getChunks().stream().map(chunk -> chunk.getContent()).toArray()));
            List<GetDocumentSplitResponseBody.GetDocumentSplitResponseBodyResultChunks> chunks = response.getBody().getResult().getChunks();
            for (int i = 0; i < chunks.size(); i++) {
                System.out.println("第" + i + "行" + chunks.get(i).getContent());
            }
        }
    }

}
